<?php
$message = array();
if (isset($_POST["commit"])) {
    $con = mysqli_connect("localhost", "root", "", "bookstore");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con, "SELECT * FROM user");

    while ($row = mysqli_fetch_array($result))
        if ($_POST['username'] == $row['username']) {
            $message[] = "Tài khoản đã được sử dụng";
        }if ($_POST['email'] == $row['email']) {
            $message[] = "Email đã được sử dụng";
        }
    mysqli_close($con);
}
?>

<script src="DangKy/js/kiemtra.js" type="text/javascript" language="javascript">
</script>

<div class="container1">
    <section class="register">
        <h1>Đăng ký tài khoản</h1>
        <form id="form1" name="DKUser" method="post" action="#" onsubmit="return KiemTraTatCa()">
            <div class="reg_section personal_info">
                <h3>Thông tin tài khoản</h3>

                <input type="text" name="username" value="" placeholder="Tên tài khoản" onblur="KTUsersName()">
                <div class="hide error" id="alertUName"></div>
                <input type="text" name="email" value="" placeholder="Email của bạn" onblur="KTEmail()">
                <div class="hide error" id="alertEmail"></div>
                <?php if (count($message) > 0): ?>
                    <?php foreach ($message as $mes): ?>
                        <p><?php echo $mes ?></p>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            <div class="reg_section password">
                <h3>Mật khẩu của bạn</h3>
                <input type="password" name="pass" value="" placeholder="Mật khẩu" onblur="KTMatKhau()">
                <div class="hide error" id="alertPass"></div>
                <input type="password" name="confirm" value="" placeholder="Mật khẩu nhập lại" onblur="KTMatKhau()">
                <div class="hide error" id="alertRePass"></div>
            </div>
            <div class="reg_section password">
                <h3>Thông tin cá nhân</h3>
                <input type="text" name="lastname" placeholder="Họ" onblur="KiemTraHo()">
                <div class="hide error" id="alertHo"></div>
                <input type="text" name="firstname" placeholder="Tên" onblur="KiemTraTen()">
                <div class="hide error" id="alertTen"></div>
                <input type="text" name="phone" placeholder="Số điện thoại" onblur="KiemTraSDT()" >
                <div class="hide error" id="alertPhone"></div>
                <input type="radio" name="sex" value="Nam">Nam <input type="radio" name="sex" value="Nữ">Nữ
                <input name="birthday"
                       onfocus="showCalendarControl(this);"
                       type="text" placeholder="Ngày sinh">
                <textarea name="address" id="" placeholder="Địa chỉ"></textarea>
            </div>
            <p class="terms">
            </p>
            <p class="submit"><input type="reset" value="Reset form"><input type="submit" name="commit" value="Đăng ký"></p>
        </form>
    </section>
</div>

